<?php

namespace app\common\model;

use think\Db;
use think\Model;


class MemberPackages extends Model
{
//    protected $auto = false;
    protected $insert = ['used_days' => 0, 'create_time', 'update_time'];
    protected $update = ['update_time'];

    /**
     * 设置添加时间
     * @return int
     */
    protected function setCreateTimeAttr()
    {
        return time();
    }

    /**
     * 设置更新时间
     * @return int
     */
    protected function setUpdateTimeAttr()
    {
        return time();
    }

    /**
     * 获取当前正在释放的最大级别
     * @param $uid
     * @return int|mixed
     */
    public static function getMaxCost($uid)
    {
        $max_package_id = MemberPackages::where('uid', $uid)->max('package_id');
        if ($max_package_id) {
            $total = Packages::where('id', $max_package_id)->value('cost');
        } else {
            $total = 0;
        }
        return $total;
    }

    public function package()
    {
        return $this->belongsTo('Packages', 'package_id', 'id');
    }
}
